#!/bin/bash
# Copyright (c) 2023 Diemit <598757652@qq.com>
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

#PROJECT_ROOT=$(cd $(dirname $0);cd ../../../../../; pwd)
export PROJECT_ROOT=/home/lixiaoxu/app
export PRODUCT_PATH=vendor/horizon/rdkx3
KERNEL_ARCH=arm64
KERNEL_VERSION=linux-4.14
KERNEL_IMAGE=Image.gz
DEFCONFIG_FILE=xj3_perf_ubuntu_defconfig
KERNEL_SRC_PATH=/home/lixiaoxu/app/kernel/linux/${KERNEL_VERSION}
KERNEL_CONFIG=${PROJECT_ROOT}/device/board/horizon/rdkx3/kernel/configs

OUT_DIR=${PROJECT_ROOT}/out
KERNEL_SRC_TMP_PATH=${OUT_DIR}/kernel/src_tmp/${KERNEL_VERSION}
KERNEL_IMAGE_FILE=${KERNEL_SRC_TMP_PATH}/arch/${KERNEL_ARCH}/boot/${KERNEL_IMAGE}

export KERNEL_OBJ_TMP_PATH=${PROJECT_ROOT}/out/kernel/OBJ/${KERNEL_VERSION}

export OUT_KERNEL_DIR=${KERNEL_OBJ_TMP_PATH}/rdkx3

PATCHES_PATH=${PROJECT_ROOT}/device/board/horizon/rdkx3/patches
HDF_PATCH_FILE=${PATCHES_PATH}/hdf.patch


mkdir -p ${KERNEL_SRC_TMP_PATH}

cp -arfL ${KERNEL_SRC_PATH}/* ${KERNEL_SRC_TMP_PATH}/
cp -arfl ${KERNEL_CONFIG}/${DEFCONFIG_FILE} ${KERNEL_SRC_TMP_PATH}/.config
  
cd ${KERNEL_SRC_TMP_PATH}

bash ${PATCHES_PATH}/hdf_patch.sh ${PROJECT_ROOT} ${KERNEL_SRC_TMP_PATH} ${HDF_PATCH_FILE}

echo "kernel tmp HDF is ready"



function pre_pkg_preinst() {
    # Get the signature algorithm used by the kernel.
    local module_sig_hash="$(grep -Po '(?<=CONFIG_MODULE_SIG_HASH=").*(?=")' "${KERNEL_SRC_TMP_PATH}/.config")"
    # Get the key file used by the kernel.
    local module_sig_key="$(grep -Po '(?<=CONFIG_MODULE_SIG_KEY=").*(?=")' "${KERNEL_SRC_TMP_PATH}/.config")"
    module_sig_key="${module_sig_key:-certs/hobot_fixed_signing_key.pem}"
    # Path to the key file or PKCS11 URI
    if [[ "${module_sig_key#pkcs11:}" == "${module_sig_key}" && "${module_sig_key#/}" == "${module_sig_key}" ]]; then
        local key_path="${KERNEL_SRC_TMP_PATH}/${module_sig_key}"
    else
        local key_path="${module_sig_key}"
    fi
    # Certificate path
    local cert_path="${KERNEL_SRC_TMP_PATH}/certs/signing_key.x509"
    # Sign all installed modules before merging.
    find ${KO_INSTALL_DIR}/lib/modules/${KERNEL_VER}/ -name "*.ko" -exec "${KERNEL_SRC_TMP_PATH}/scripts/sign-file" "${module_sig_hash}" "${key_path}" "${cert_path}" '{}' \;
}

#rm -rf ${OUT_DIR}/kernel
#bash check_patch.sh ${PROJECT_ROOT} ${KERNEL_VERSION}
#bash make_kernel.sh ${PROJECT_ROOT} ${PRODUCT_PATH} ${KERNEL_ARCH} ${KERNEL_VERSION} ${KERNEL_IMAGE} ${DEFCONFIG_FILE} ${OUT_PKG_DIR}
export CROSS_COMPILE=/home/lixiaoxu/app/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu- 
export LD_LIBRARY_PATH=/home/lixiaoxu/app/prebuilts/gcc/linux-x86/aarch64/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/lib/
#export PATH=$PATH:/opt/gcc-ubuntu-9.3.0-2020.03-x86_64-aarch64-linux-gnu/bin/
export ARCH=arm64

cd ${KERNEL_SRC_TMP_PATH}
echo "start make zImage.lz4 and  dtb.img!"

make ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} 

make ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE}  modules_prepare

make ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} ${KERNEL_IMAGE} dtbs -j 6
# echo "start make zImage.lz4 over!!"
#echo "start make modules prepare!"
mkdir -p ${OUT_KERNEL_DIR}/dtb
KO_INSTALL_DIR=${OUT_KERNEL_DIR}/modules
    [ ! -d $KO_INSTALL_DIR ] && mkdir -p $KO_INSTALL_DIR
    rm -rf $KO_INSTALL_DIR/*
echo "start make modules !"
make CROSS_COMPILE=${CROSS_COMPILE} INSTALL_MOD_PATH=$KO_INSTALL_DIR INSTALL_MOD_STRIP=1 modules_install -j 6
find ${KO_INSTALL_DIR}/lib/modules/${KERNEL_VER}/ -name "*.ko" -exec ${CROSS_COMPILE}strip -v -g '{}' \;

rm -rf ${KO_INSTALL_DIR}/lib/modules/${KERNEL_VER}/{build,source}

cp -f "arch/arm64/boot/${KERNEL_IMAGE}" ${OUT_KERNEL_DIR}/
# 拷贝 内核 Image

cp -f "arch/arm64/boot/Image" ${OUT_KERNEL_DIR}/
#拷贝编译设备信息文件
mkdir -p ${KERNEL_OBJ_TMP_PATH}/rdkx3/dtb
cp -arf arch/arm64/boot/dts/hobot/*.dtb ${OUT_KERNEL_DIR}/dtb
cp -arf arch/arm64/boot/dts/hobot/*.dts ${OUT_KERNEL_DIR}/dtb
cp -arf arch/arm64/boot/dts/hobot/*.dtsi ${OUT_KERNEL_DIR}/dtb


# ko 签名
pre_pkg_preinst

path=./tools/dtbmapping

cd $path

export TARGET_KERNEL_DIR=${OUT_KERNEL_DIR}/dtb
    # build dtb
python2 makeimg.py || {
    echo "make failed"
    exit 1
}

IMAGE_SIZE=64  # 64M
IMAGE_BLOCKS=4096

blocks=${IMAGE_BLOCKS}
block_size=$((${IMAGE_SIZE} * 1024 * 1024 / ${blocks}))


#echo y | sudo mke2fs -b ${block_size} -d boot_linux -i 8192 -t ext2 boot_linux.img ${blocks}
#make ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} modules_prepare

#make ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} ${KERNEL_IMAGE} dtbs -j 6

#make ARCH=${ARCH} CROSS_COMPILE=${CROSS_COMPILE} modules -j 6



